import {expect} from 'chai';
import {graphql} from 'graphql';
import {Source} from 'graphql/language/source';
import {parse} from 'graphql/language/parser';
import {validate} from 'graphql/validation/validate';
import schema from '../../src/app/_';

function check(query) {
    const src = new Source(query);
    const ast = parse(src);
    return validate(schema, ast);
}

const query = `
    query {
        hello {
            text
            index
        }
    }
`;

const query_invalid = `
    query {
        hello {
            name
        }
    }
`;

describe('hello', () => {
    context('check', () => {
        it('valid', () => expect(check(query)).to.be.empty);

        it('invalid', () => expect(check(query_invalid)).to.throw);
    });

    context('query', () => {
        // 默认的graphql似乎无法处理apollo的schema（和resolver有关？）
        it('default', () => graphql(schema, query).then(({data}) => {
            const {hello: x} = data;
            expect(x).to.be.an('object');
            expect(x).to.has.property('text');
            expect(x).to.has.property('index');
            expect(x.text).to.eq('Matrix');
            expect(x.index).to.eq(1);
        }));
    });
});
